package com.smart4s.app.core.positionservice.utils;

import android.content.Context;

import com.smart4s.app.core.positionservice.MyApp.App;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 数据配置管理
 */
public class ConfigManager {
    //Assets 配置文件是否覆盖本地配置文件
    private static final boolean IS_ASSETS_COVER_CACHE = true;
    // Assets 目录下默认配置目录
    private static final String ASSETS_MARKET_DATA_PATH = "default";
    //光定位自定义配置文件
    public static final String DECODER_CUSTOM_PARAMS_JSON = "decoder_custom_params.json";
    // 光定位硬件配置文件
    public static final String DECODER_HW_PARAMS_JSON = "decoder_hw_params.json";
    // 地图配置文件
    public static final String MAP_PARAMS_JSON = "map_params.json";
    // 灯数据配置文件
    public static final String LIGHT_INFO_JSON = "led_info.json";

    // 获取缓存目录
    //@return 缓存目录
    private static String getCachePath() {
        return App.getContext().getCacheDir().getPath() + "cache/";
    }

    // 获取配置文件路径
    // @param fileName 文件名
    public static String getFilePath(String fileName) {
        return getCachePath() + fileName;
    }

    //释放应用自带的超市数据至缓存目录
    // DEBUG 模式下覆盖
    public static void releaseAssetsMarketData(Context context) throws IOException {
        // 检查目录是否存在，不存在则创建
        File file = context.getCacheDir();
        if (!file.exists() && !file.mkdirs()) return;

        String[] filePaths = context.getAssets().list(ASSETS_MARKET_DATA_PATH);
        // 循环拷贝，如果已有该文件则不覆盖
        for (String filePath : filePaths) {
            copyFileFromAssets(context,
                    ASSETS_MARKET_DATA_PATH + File.separator + filePath,
                    getFilePath(filePath), IS_ASSETS_COVER_CACHE);
        }
    }

    private static void copyFileFromAssets(Context context, String assetsFilePath, String targetFileFullPath, boolean isCover) throws IOException {
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            File file = new File(targetFileFullPath);
            String dir = file.getParent();
            File fileDir = new File(dir);
            if (!fileDir.exists() && !fileDir.mkdirs()) {
                // 不存在且创建不成功
                return;
            }
            if (isCover || !file.exists()) {
                inputStream = context.getAssets().open(assetsFilePath);
                fileOutputStream = new FileOutputStream(file);
                copy(inputStream, fileOutputStream);
                return;
            }
        } catch (IOException var12) {
            throw var12;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }

            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
    }

    private static void copy(InputStream is, OutputStream os) throws IOException {
        byte[] var2 = new byte[1024];
        boolean var3 = false;

        int var4;
        while ((var4 = is.read(var2)) != -1) {
            os.write(var2, 0, var4);
        }
    }
}
